package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.JsolaException;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendanceDedProgAddDTO;
import org.jsola.hr.dto.AttendanceDedProgUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.AttendanceDedProgQuery;
import org.jsola.hr.service.IAttendanceDedProgService;
import org.jsola.hr.vo.AttendanceDedProgListVO;
import org.jsola.hr.vo.AttendanceDedProgVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrAttendanceDedProgController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendanceDedProgs")
public class AttendanceDedProgController implements IAttendanceDedProgControllerAPI {

    @Autowired
    private IAttendanceDedProgService attendanceDedProgService;

    @Override
    @PostMapping
    public Result<AttendanceDedProgVO> save(@Valid @RequestBody AttendanceDedProgAddDTO attendanceDedProgAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工考勤扣款方案权限校验
        try {
            return Result.success(
                    attendanceDedProgService.save(attendanceDedProgAddDTO, tokenUser)
            );
        } catch(HrException he){
            Result result = Result.fail(he.getMessage());
            return result;
        } catch (JsolaException je) {
            Result result = Result.fail("conflict");
            result.setData(je.getMessage());
            return result;
        }

    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendanceDedProgUpdateDTO attendanceDedProgUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工考勤扣款方案权限校验

        try {
            int count = attendanceDedProgService.update(attendanceDedProgUpdateDTO, tokenUser);
            if (count == 0) {
                throw new ParamException("更新失败，请刷新页面后重试");
            }
            return Result.success(count);
        } catch (JsolaException je) {
            Result result = Result.success("conflict");
            result.setData(je.getMessage());
            return result;
        }

    }



    @Override
    @DeleteMapping(value = "/{attendanceDedProgId}")
    public Result<Integer> delete(@PathVariable String attendanceDedProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工考勤扣款方案权限校验

        int count = attendanceDedProgService.deleteByIds(tokenUser, attendanceDedProgId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendanceDedProgIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工考勤扣款方案权限校验

        int count = attendanceDedProgService.deleteByIds(tokenUser, attendanceDedProgIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendanceDedProgId}")
    public Result<AttendanceDedProgVO> findById(@PathVariable String attendanceDedProgId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤扣款方案权限校验

        return Result.success(
            attendanceDedProgService.selectById(attendanceDedProgId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendanceDedProgListVO>> listAll(@Valid AttendanceDedProgQuery attendanceDedProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤扣款方案权限校验

        return Result.success(
            attendanceDedProgService.select(attendanceDedProgQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendanceDedProgListVO>> page(@Valid AttendanceDedProgQuery attendanceDedProgQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendanceDedProgService.selectPage(attendanceDedProgQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectListAll")
    public Result<List<AttendanceDedProgListVO>> selectListAll(@Valid String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤扣款方案权限校验

        return Result.success(
                attendanceDedProgService.selectListAll(companyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/selectRelationById")
    public Result<AttendanceDedProgVO> selectRelationById(@Valid String id) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工考勤扣款方案权限校验

        return Result.success(
                attendanceDedProgService.selectRelationById(id, tokenUser.getSiteId())
        );
    }


}

